﻿using System.Collections.Generic;
using UnityEngine;

public class UIManager
{
    private Dictionary<UIType, GameObject> uiDict = new Dictionary<UIType, GameObject>();
    private Transform __transform { get;  set; }
    public UIManager()
    {
        __transform = GameObject.Find("Canvas").transform;
    }
    public GameObject GetSingleUI(UIType uiType)
    {
        Transform parent = uiType.Parent;
        if (parent == null)
            parent = __transform;
        if(!uiDict.ContainsKey(uiType) || uiDict[uiType]==null)
        {
            GameObject go = GameObject.Instantiate(Resources.Load(uiType.Path)) as GameObject;
            go.transform.SetParent(parent,false);
            uiDict.Add(uiType, go);
        }
        return uiDict[uiType];
    }
    public void DestroySingleUI(UIType uiType)
    {
        if (!uiDict.ContainsKey(uiType))
            return;
        if(uiDict[uiType] == null)
        {
            uiDict.Remove(uiType);
            return;
        }
        GameObject.Destroy(uiDict[uiType]);
        uiDict.Remove(uiType);
    }
   
}
